package model

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
)

var db *sql.DB

func init()  {
	var err error
	//如果需要对时间进行解析，增加parseTime=true的操作
	db,err = sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=true")
	if err != nil {
		panic(err)
	}
	db.SetMaxOpenConns(100)
	db.SetMaxIdleConns(50)

}

func Exec(query string, args ...interface{}) (sql.Result, error)  {
	return db.Exec(query, args)
}

func Querys(query string) (*sql.Rows, error)  {
	return db.Query(query)
}

func Save(query string, args ...interface{}) (int64, error)  {
	state,err := db.Prepare(query)
	if err != nil {
		return 0,err
	}
	res,err := state.Exec(args)
	if err != nil {
		return 0,err
	}
	id,err := res.LastInsertId()
	if err != nil {
		return 0,err
	}
	return id,nil

}
